﻿Imports System.Object
Imports VSModel
Imports Cls_Common
Imports System.IO
Imports System.Data
Partial Class FrontEnd_Control_Home_ProductNewsHome
    Inherits System.Web.UI.UserControl
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            LoadData()
        End If
    End Sub
    Private Sub LoadData()
        Using Data As New VSEntities
            'Add DomainName DUC 09-09-2014
            Dim p = (From q In Data.Products Where q.Domain = DomainName And q.StatusId = 3 Order By q.PublishedDate Descending Select q).Take(6).ToList
            With rptProduct
                .DataSource = p
                .DataBind()
            End With
        End Using
    End Sub
    Protected Sub rptProduct_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptProduct.ItemDataBound
        Dim row As VSModel.Product = CType(e.Item.DataItem, VSModel.Product)
        Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)
        hplTitle.Text = row.Title
        hplTitle.CssClass = "title-thumbnail"
        hplTitle.NavigateUrl = MainDomain & row.CateID & "/" & row.QueryString & "-" & row.ProductId & ".html"

        Dim hplLink As HyperLink = CType(e.Item.FindControl("hplLink"), HyperLink)
        hplLink.NavigateUrl = MainDomain & row.CateID & "/" & row.QueryString & "-" & row.ProductId & ".html"

        Dim imgImages As Image = CType(e.Item.FindControl("imgImages"), Image)
        imgImages.ImageUrl = row.ImagePreview

        Dim ltrTenGoi As Literal = CType(e.Item.FindControl("ltrTenGoi"), Literal)
        ltrTenGoi.Text = row.Title
        Dim ltrKiHieu As Literal = CType(e.Item.FindControl("ltrKiHieu"), Literal)
        ltrKiHieu.Text = row.Code
        Dim ltrXuatSu As Literal = CType(e.Item.FindControl("ltrXuatSu"), Literal)
        ltrXuatSu.Text = row.XuatXu
        Dim ltrGia As Literal = CType(e.Item.FindControl("ltrGia"), Literal)
        ltrGia.Text = " Call" ' row.OriginalPrice

        Dim hplChiTiet As HyperLink = CType(e.Item.FindControl("hplChiTiet"), HyperLink)
        hplChiTiet.CssClass = "btn btn-success btn-sm"
        hplChiTiet.Attributes.Add("Role", "button")
        hplChiTiet.NavigateUrl = MainDomain & row.CateID & "/" & row.QueryString & "-" & row.ProductId & ".html"
        'Dim lbtGioHang As LinkButton = CType(e.Item.FindControl("lbtGioHang"), LinkButton)
        'lbtGioHang.CssClass = "btn btn-default btn-sm"
        'lbtGioHang.Attributes.Add("Role", "button")
    End Sub
    Protected Sub lbtGioHang_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        'Dim _ProductID As Integer
        '_ProductID = sender.tooltip()
        'AddShoppingCart(_ProductID)
        Session("LastURL") = Request.Url
        Response.Redirect(MainDomain & "gio-hang.html")


    End Sub

    Private Sub AddShoppingCart(ByVal _ProductID As String)

        Using Data As New VSEntities
            Dim _cartID As String = Session("CartId")
            'Add DomainName DUC 09-09-2014
            Dim check = (From q In Data.ShoppingCarts Where q.Domain = DomainName And q.CartId = _cartID And q.ProductId = _ProductID Select q).FirstOrDefault
            If check Is Nothing Then
                Dim p As New ShoppingCart
                p.CartId = _cartID
                p.ProductId = _ProductID
                p.UserId = Session("UserId")
                ' p.StoreId = (From q In Data.Products Where q.ProductId = _ProductID Select q.StoreId).FirstOrDefault
                p.Quantity = 1
                Data.ShoppingCarts.AddObject(p)
                Data.SaveChanges()

            End If
        End Using
    End Sub


End Class
